import { renderNormalCell } from "./cells/normal-cell.mjs";
import { renderEditCell } from "./cells/edit-cell.mjs";
import { renderCellStyle } from "./cells/cell-style.mjs";
import { renderActionButton } from "./cells/action-button.mjs";

/**
 * 渲染表格行
 * @param {Object} params - 渲染参数
 * @param {HTMLElement} params.tr - tr元素
 * @param {Object} [params.row] - 行数据
 * @param {Object} [params.template] - 模板配置
 * @param {string} [params.mode='read'] - 渲染模式
 * @param {boolean} [params.insert=false] - 是否为插入行
 */
export function renderRow({ tr, row, template, mode, insert = false }) {
  if (template.debug) console.log(row);

  // 渲染常规列
  template.gridview?.forEach((col) => {
    let td = document.createElement("td");
    tr.appendChild(td);

    // 处理单元格样式
    renderCellStyle(td, col, row);

    // 判断渲染模式
    const shouldRenderEditCol =
      template.rowedit &&
      mode === "edit" &&
      !/\[\[[\w/.]+\]\]/.test(col.col) &&
      !(col.readonly && !insert);

    // 渲染单元格内容
    if (shouldRenderEditCol) {
      renderEditCell({ td, col, template, insert });
    } else {
      renderNormalCell({ td, col, tr, template });
    }
  });

  // 渲染操作按钮列
  if ((template.rowinsert || template.rowdelete) && mode === "edit") {
    renderActionButton({ tr, template, mode, insert });
  }
}
